﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Xml.Linq;
using DVK.SP2007.UniversalClient;
using DVK.SP2007.Constants;
using System.Xml;


namespace DVK.SP2007
{
    /// <summary>
    /// Maps data from some data to IDvkMessage.
    /// </summary>
    public class OutgoingMessageToDvkMessageMapper : IMessageMapper<IDvkMessage>
    {
        private SPListItem _listItem;
        private IDvkResourceManager _resourceManager;
        public OutgoingMessageToDvkMessageMapper(SPListItem outboxItem, IDvkResourceManager resourceManager)
        {
            _listItem = outboxItem;
            _resourceManager = resourceManager;
        }

        #region IMessageConverter<SPListItem> Members

        /// <summary>
        /// Maps metadata from somewhere into the object instance of type T.
        /// <remarks>
        /// Normally this means mapping information from different location into one format (object).
        /// e.g. to IDvkMessage or Inbox SPListItem.
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public IDvkMessage Map()
        {
            IDvkMessage message = new DvkMessage();
            message.FolderName = _listItem[Fields.FolderName.Name] as string;
            message.CaseName = _listItem[Fields.CaseName.Name] as string;
            message.LocalItemId = _listItem.ID;
            message.IsIncoming = false;
            message.Title = _listItem.Title;
            message.UnitId = _listItem[Fields.UnitId.Name] as int?;
            message.Sender = new SenderReceiverInfo()
            {
                Organisation = new UnitInfo()
                {
                    Code = _listItem[Fields.SenderOrganisationCode.Name] as string,
                    Name = _listItem[Fields.SenderOrganisationName.Name] as string
                },
                Person = new UnitInfo()
                {
                    Code = _listItem[Fields.SenderPersonCode.Name] as string,
                    Name = _listItem[Fields.SenderPersonName.Name] as string
                }
            };

            message.SendingDate = _listItem[Fields.SendingDate.Name] as DateTime?;
            message.SendingStatus = DvkSendingStatus.Waiting;
            message.StatusUpdateNeeded = (bool)_listItem[Fields.StatusUpdateNeeded.Name];

            var metaXml = _listItem[Fields.DvkCustomMetaXml.Name] as string;
            if (!string.IsNullOrEmpty(metaXml))
                message.MetaXml = XElement.Parse(metaXml);

            message.DataXml = new DvkXml(
                                new XElement(
                                    DvkXml.ElementNameDvkDocument,
                                    _listItem[Fields.DvkMetainfoXml.Name] != null ? XElement.Parse(_listItem[Fields.DvkMetainfoXml.Name] as string) : null,
                                    _listItem[Fields.DvkTransportXml.Name] != null ? XElement.Parse(_listItem[Fields.DvkTransportXml.Name] as string) : null,
                                    _listItem[Fields.DvkMetaXml.Name] != null ? XElement.Parse(_listItem[Fields.DvkMetaXml.Name] as string) : null
                                    )
                            );

            //Get the signeddoc in bytes
            SPFile file = _listItem.File;
            message.DataXml.SignedDocInBytes = file.OpenBinary();

            return message;
        }

        #endregion
    }
}
